import { request } from "../request";

/**
 * 人口统计学信息接口
 */
export interface DemographicInfo {
  id?: string;
  phone: string;
  name?: string;
  age?: number;
  gender?: string;
  address?: string;
  occupation?: string;
  education?: string;
  maritalStatus?: string;
  income?: number;
  familySize?: number;
  healthStatus?: string;
  medicalHistory?: string;
  insuranceType?: string;
  emergencyContact?: string;
  emergencyPhone?: string;
  createdAt?: string;
  updatedAt?: string;
}

/**
 * 根据电话号码获取人口统计学信息
 * @param phone 电话号码
 * @returns Promise<DemographicInfo>
 */
export function getDemographicInfo(phone: string) {
  console.log(`根据电话号码${phone}获取人口统计学信息`);
  return request.get<DemographicInfo>(
    "/health_records/HealthRecord/healthRecordInfo/list",
    {
      // idNumber:idcard,
      tel: phone,
    },
    {
      useErrMsg: true,
    }
  );
}

export function getNurseUserByPhone(phone: string) {
  return request.get<DemographicInfo>(
    "/nurse_user/nurseUser/list",
    {
      phone: phone,
    },
    {
      useErrMsg: true,
    }
  );
}

/**
 * 创建人口统计学信息
 * @param data 人口统计学信息数据
 * @returns Promise<DemographicInfo>
 */
export function createDemographicInfo(data: Omit<DemographicInfo, "id" | "createdAt" | "updatedAt">) {
  return request.post<DemographicInfo>("/demographic/info/create", data, {
    useErrMsg: true,
  });
}

/**
 * 更新人口统计学信息
 * @param data 人口统计学信息数据
 * @returns Promise<DemographicInfo>
 */
export function updateDemographicInfo(data: DemographicInfo) {
  return request.put<DemographicInfo>("/demographic/info/update", data, {
    useErrMsg: true,
  });
}

/**
 * 删除人口统计学信息
 * @param id 信息ID
 * @returns Promise<void>
 */
export function deleteDemographicInfo(id: string) {
  return request.delete(
    `/demographic/info/delete?id=${id}`,
    {},
    {
      useErrMsg: true,
    }
  );
}

/**
 * 获取人口统计学信息列表
 * @param params 查询参数
 * @returns Promise<{list: DemographicInfo[], total: number}>
 */
export function getDemographicInfoList(params: { pageNo?: number; pageSize?: number; phone?: string; name?: string }) {
  const queryParams = new URLSearchParams();
  if (params.pageNo) queryParams.append("pageNo", params.pageNo.toString());
  if (params.pageSize) queryParams.append("pageSize", params.pageSize.toString());
  if (params.phone) queryParams.append("phone", params.phone);
  if (params.name) queryParams.append("name", params.name);

  return request.get<{ list: DemographicInfo[]; total: number }>(
    `/demographic/info/list?${queryParams.toString()}`,
    {},
    {
      useErrMsg: true,
    }
  );
}
